package com.shop.item.api.controller;

import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.item.ItemCollectionInput;
import com.shop.common.model.param.item.ItemCommentInput;
import com.shop.common.model.param.item.ItemFootInput;
import com.shop.common.model.result.item.ItemCollectionOutput;
import com.shop.common.model.result.item.ItemFootOutput;
import com.shop.common.tmpl.BaseController;
import com.shop.common.util.SessionUtils;
import com.shop.common.util.excption.UserNotFoundException;
import com.shop.item.api.service.IItemExtService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/item/ext")
public class ItemExtController extends BaseController {
    @Autowired
    SessionUtils sessionUtils;
    @Autowired
    IItemExtService itemExtService;

    @GetMapping("/itemExt")
    public String apiSelectItemExt(String itemId) {
        ServiceBean serviceBean = itemExtService.selectItemExt(Integer.valueOf(itemId));
        return returnData(serviceBean.getData());
    }

    /**
     * @Author zhoulk
     * @Description //TODO 收藏/取消收藏
     * @Date 23:28 2019/3/20
     *
     * @Param [itemId, request]
     * @return java.lang.String
     * @errorCode //TODO
     **/
    @PutMapping("/collect")
    public String apiCollectItem(String itemId, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        ItemCollectionInput itemCollectionInput = new ItemCollectionInput();
        itemCollectionInput.setItemId(Integer.valueOf(itemId));
        itemCollectionInput.setUserId(userId);
        ServiceBean serviceBean = itemExtService.collectionItem(newParamMode(itemCollectionInput));
        if (serviceBean.isSuccess()) {
            return returnSuccess();
        }
        return returnFailed();
    }

    /**
     * @Function com.shop.item.api.controller.ItemExtController.apiCancelCollection
     * @Description //TODO 取消收藏
     *
     * @param: itemId
     * @param: request
     * @return java.lang.String
     * @errorCode //TODO
     *
     * @Author zhoulk
     * @Date
     *
     **/
    @DeleteMapping("/collect")
    public String apiCancelCollection(String itemId, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        ItemCollectionInput itemCollectionInput = new ItemCollectionInput();
        itemCollectionInput.setItemId(Integer.valueOf(itemId));
        itemCollectionInput.setUserId(userId);
        ServiceBean serviceBean = itemExtService.cancelCollection(newParamMode(itemCollectionInput));
        if (serviceBean.isSuccess()) {
            return returnSuccess();
        }
        return returnFailed();
    }

    /**
     * @Author zhoulk
     * @Description //TODO 是否收藏
     * @Date 23:28 2019/3/20
     *
     * @Param [itemId, request]
     * @return java.lang.String
     * @errorCode //TODO
     **/
    @GetMapping("/isCollect")
    public String apiIsCollect(String itemId, HttpServletRequest request) {
        Integer userId = null;
        try {
            userId = sessionUtils.USER_ID(request);
        } catch (UserNotFoundException e) {
            return returnFailed();
        }
        ItemCollectionInput itemCollectionInput = new ItemCollectionInput();
        itemCollectionInput.setId(Integer.valueOf(itemId));
        itemCollectionInput.setUserId(userId);
        ServiceBean serviceBean = itemExtService.isCollection(newParamMode(itemCollectionInput));
        if (serviceBean.isSuccess()) {
            return returnSuccess();
        }
        return returnFailed();
    }

    /**
     * @Author zhoulk
     * @Description //TODO 查询用户收藏列表
     * @Date 23:35 2019/3/20
     *
     * @Param [index, pageSize, request]
     * @return java.lang.String
     * @errorCode //TODO
     **/
    @GetMapping("/collectList")
    public String apiSelectCollectList(ItemCollectionInput itemCollectionInput, Integer index, Integer pageSize, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        InputBean inputBean = new InputBean();
        inputBean.setIndex(index);
        inputBean.setPageSize(pageSize);
        itemCollectionInput.setUserId(userId);
        inputBean.setParam(itemCollectionInput);
        ServiceBean<ItemCollectionOutput> serviceBean = itemExtService.selectItemCollectionList4P(inputBean);
        if (serviceBean.isSuccess()) {
            return returnData(serviceBean.getData());
        }
        return returnFailed();
    }

    /**
     * @Author zhoulk
     * @Description //TODO 评论商品
     * @Date 23:28 2019/3/20
     *
     * @Param [commentInput, request]
     * @return java.lang.String
     * @errorCode //TODO
     **/
    @PostMapping("/comment")
    public String apiAddComment(@RequestBody ItemCommentInput commentInput, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        commentInput.setUserId(userId);
        ServiceBean serviceBean = itemExtService.insertItemComment(newParamMode(commentInput));
        if (!serviceBean.isSuccess()) {
            return returnFailed(serviceBean.getMsg());
        }
        return returnSuccess();
    }

    /**
     * @Author zhoulk
     * @Description //TODO 商品评论列表
     * @Date 23:28 2019/3/20
     *
     * @Param [itemId, index, pageSize, request]
     * @return java.lang.String
     * @errorCode //TODO
     **/
    @GetMapping("/comments")
    public String apiSelectCommentList(String itemId, Integer index, Integer pageSize, HttpServletRequest request) {
        if (StringUtils.isBlank(itemId)) {
            returnFailed("参数错误");
        }
        InputBean<Integer> inputBean = new InputBean();
        inputBean.setIndex(index);
        inputBean.setPageSize(pageSize);
        inputBean.setParam(Integer.valueOf(itemId));
        ServiceBean serviceBean = itemExtService.selectItemCommentList4P(inputBean);
        if (serviceBean.isSuccess()) {
            Map<String, Object> map = new HashMap<>();
            map.put("total", serviceBean.getData());
            map.put("count", serviceBean.getMaxSize());
            return returnData(map);
        }
        return returnFailed();
    }

    /**
     * @Author zhoulk
     * @Description //TODO 查询用户足迹
     * @Date 23:29 2019/3/20
     *
     * @Param [request]
     * @return java.lang.String
     * @errorCode //TODO
     **/
    @GetMapping("/foots")
    public String apiSelectFoot(Integer index, Integer pageSize, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        InputBean inputBean = new InputBean();
        inputBean.setIndex(index);
        inputBean.setPageSize(pageSize);
        ItemFootInput itemFootInput = new ItemFootInput();
        itemFootInput.setUserId(userId);
        inputBean.setParam(itemFootInput);
        ServiceBean<List<ItemFootOutput>> serviceBean = itemExtService.selectItemFootList4P(inputBean);
        if (serviceBean.isSuccess()) {
            return returnData(serviceBean.getData());
        }
        return returnFailed();
    }

    /**
     * @Function com.shop.item.api.controller.ItemExtController.apiDeleteFoot
     * @Description //TODO 删除浏览足迹
     *
     * @param: id
     * @param: request
     * @return java.lang.String
     * @errorCode //TODO
     *
     * @Author zhoulk
     * @Date
     *
     **/
    @DeleteMapping("/foot")
    public String apiDeleteFoot(String id, HttpServletRequest request) {
        if (StringUtils.isBlank(id)) {
            returnFailed("参数异常");
        }
        ServiceBean serviceBean = itemExtService.deleteFoot(newParamMode(Integer.valueOf(id)));
        if (serviceBean.isSuccess()) {
            return returnSuccess();
        }
        return returnFailed();
    }

}
